package com.atguigu.srb.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.ArrayList;
import java.util.List;

/**
 * 跨域配置
 * @ClassName CorsConfig
 * @Author ZengRJ
 * @Date 2022/9/9 17:32
 * @Version 1.0
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); //允许携带cookie
        List<String> allowedOrigins = new ArrayList<String>();
        allowedOrigins.add("*");
        config.setAllowedOrigins(allowedOrigins);

        List<String> allowedHeaders = new ArrayList<String>();
        allowedHeaders.add("*");
        config.setAllowedHeaders(allowedHeaders);

        List<String> allowedMethods = new ArrayList<String>();
        allowedMethods.add("*");
        config.setAllowedMethods(allowedMethods);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",config);
        return new CorsWebFilter(source);
    }
}
